package com.jiyun.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.ConsumerMapper;
import com.jiyun.mapper.CourseMapper;
import com.jiyun.model.Consumer;
import com.jiyun.model.Course;
import com.jiyun.service.ConsumerService;
import com.jiyun.vo.ResultResopnse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/* *
 * @Author nxj
 * @Date 2023 08 2023/8/22 10 53
 * */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public ResultResopnse addGood(Integer id) {
        Integer id1 = (Integer) redisTemplate.opsForValue().get("id");

        Object course1 = redisTemplate.opsForHash().get(id1+"", id + "");
        if(course1==null){
            Course course = courseMapper.selectById(id);
            course.setGood(course.getGood()+1);
            courseMapper.updateById(course);
            redisTemplate.opsForHash().put(id1+"",id+"",JSON.toJSONString(course.getGood()));
            return ResultResopnse.success(1);
        }else {

            Course course = courseMapper.selectById(id);
            course.setGood(course.getGood()-1);
            courseMapper.updateById(course);
            redisTemplate.opsForHash().delete(id1+"",id+"");
            return ResultResopnse.success(2);
        }



    }
}
